package com.zy.garbage.controller;

import com.zy.garbage.enums.PermsEnum;
import com.zy.garbage.enums.ResultEnum;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.ExamSimpleForm;
import com.zy.garbage.service.QuestionService;
import com.zy.garbage.util.ResultUtil;
import com.zy.garbage.vo.ExamVO;
import com.zy.garbage.vo.QuestionListVO;
import com.zy.garbage.vo.QuestionStatisticVO;
import com.zy.garbage.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/question")
public class QuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/showByExam/{examId}")
    public ResultVO showByExam(@PathVariable("examId") Integer examId, HttpServletResponse response) {
        try {
            QuestionListVO questionListVO = questionService.findByExamId(examId);
            return ResultUtil.success(questionListVO);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @PutMapping("/updateAnswer")
    public ResultVO showByExam(@Validated @RequestBody ExamSimpleForm examSimpleForm, HttpServletResponse response) {
        try {
            QuestionListVO questionListVO = questionService.update(examSimpleForm);
            return ResultUtil.success(questionListVO);
        } catch (CommonException e) {
            throw new CommonException(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("/statistic")
    public ResultVO statistic() {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<QuestionStatisticVO> questionStatisticVOList = questionService.statistic();
                return ResultUtil.success(questionStatisticVOList);
            } catch (CommonException e) {
                throw new CommonException(500, "查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }
}
